package com.hnichr.ump.ecard.controller;


import com.hnichr.ump.common.ecard.entity.EcardCompany;
import com.hnichr.ump.common.ecard.service.EcardCompanyService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ecard/company/v1/")
@Tag(name = "CompanyController", description = "电子名片公司维护接口")
public class CompanyController {

    @Autowired
    private EcardCompanyService companyService;
    
    
    @GetMapping("list")
    @Operation(summary = "公司列表", description = "获取公司列表（无分页）", method = "GET")
    public Result<List<EcardCompany>> list(@Parameter(description="查询参数") Criteria criteria){
        List<EcardCompany> list = companyService.list(EcardCompany.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "公司列表", description = "公司列表（分页）", method = "GET")
    public Result<Page<EcardCompany>> page(@Parameter(description="查询参数")  Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo){
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<EcardCompany> list = companyService.listPage(EcardCompany.class,page, criteria);
        return Result.OK(list);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取公司", description = "根据ID获取公司", method = "GET")
    public Result<EcardCompany> getById(@Parameter(description="ID") @PathVariable(name = "id", required = true) String id) {
        EcardCompany company = companyService.getById(EcardCompany.class, id);
        return Result.OK(company);
    }

    @PostMapping("add")
    @Operation(summary = "新增公司", description = "新增公司", method = "POST")
    public Result<EcardCompany> add(@RequestBody EcardCompany company) {
        companyService.save(company);
        return Result.OK(company);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑公司", description = "编辑公司", method = "PUT")
    public Result<EcardCompany> edit(@RequestBody EcardCompany company) {
        companyService.update(company);
        return Result.OK(company);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除公司", description = "根据id删除公司", method = "DELETE")
    public Result<EcardCompany> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        String msg = companyService.remove(id);
        if(StringUtils.isNotBlank(msg)){
            return Result.error(msg);
        }
        return Result.OK();
    }
    
}

